<template>
  <div class="scene-page">
    <!-- 场景容器 -->
    <div class="scene-container" v-if="!loading">
      <!-- 左侧场景列表 -->
      <div class="scene-sidebar">
        <div class="scene-nav">
          <div class="nav-title">场景</div>
          <div class="scene-list">
            <div 
              v-for="scene in scenes" 
              :key="scene.sceneId"
              class="scene-item"
              :class="{ active: selectedScene && selectedScene.sceneId === scene.sceneId }"
              @click="selectScene(scene)"
            >
              <div class="scene-name">{{ scene.sceneName }}</div>
              <div class="scene-type">{{ scene.sceneType }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧场景详情 -->
      <div class="scene-main-content">
        <div class="scene-display" v-if="selectedScene">
          <!-- 场景背景图 -->
          <div class="scene-background">
            <img 
              :src="selectedScene.sceneImageUrl || defaultSceneImage" 
              :alt="selectedScene.sceneName"
              class="scene-bg-image"
            />
            <div class="scene-overlay"></div>
          </div>

          <!-- 场景信息 -->
          <div class="scene-info">
            <div class="scene-title">{{ selectedScene.sceneName }}</div>
            <div class="scene-description" v-if="selectedScene.backgroundStory">
              {{ selectedScene.backgroundStory }}
            </div>
            
            <!-- 地标列表 -->
            <div class="landmarks-section" v-if="landmarks.length > 0">
              <div class="landmarks-title">地标探索</div>
              <div class="landmarks-grid">
                <div 
                  v-for="landmark in landmarks" 
                  :key="landmark.landmarkId"
                  class="landmark-item"
                  @click="showLandmarkDetail(landmark)"
                >
                  <div class="landmark-image">
                    <img 
                      :src="landmark.landmarkImageUrl || defaultLandmarkImage" 
                      :alt="landmark.landmarkName"
                    />
                  </div>
                  <div class="landmark-name">{{ landmark.landmarkName }}</div>
                </div>
              </div>
            </div>

            <!-- 播放按钮 -->
            <div class="scene-actions">
            
            </div>
          </div>

          <!-- 分页指示器 -->
          <div class="scene-pagination">
            <span class="page-indicator">{{ currentSceneIndex + 1 }} / {{ scenes.length }}</span>
          </div>
        </div>

        <!-- 默认状态 -->
        <div v-else class="scene-placeholder">
          <div class="placeholder-content">
            <div class="placeholder-icon">🏞️</div>
            <div class="placeholder-text">选择一个场景开始探索</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载场景中...</p>
    </div>

    <!-- 错误状态 -->
    <div v-if="error" class="error-container">
      <div class="error-icon">⚠️</div>
      <p class="error-message">{{ error }}</p>
      <button class="retry-btn" @click="loadScenes">重试</button>
    </div>

    <!-- 地标详情弹窗 -->
    <div v-if="showLandmarkModal" class="landmark-modal" @click="closeLandmarkModal">
      <div class="landmark-modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ selectedLandmark?.landmarkName }}</h3>
          <button class="close-btn" @click="closeLandmarkModal">×</button>
        </div>
        <div class="modal-body">
          <div class="landmark-detail-image">
            <img 
              :src="selectedLandmark?.landmarkImageUrl || defaultLandmarkImage" 
              :alt="selectedLandmark?.landmarkName"
            />
          </div>
          <div class="landmark-detail-info">
            <p v-if="selectedLandmark?.landmarkDesc">{{ selectedLandmark.landmarkDesc }}</p>
            <div class="landmark-meta">
              <div class="meta-item" v-if="selectedLandmark?.visualResourceId">
                <span class="meta-label">视觉资源ID：</span>
                <span class="meta-value">{{ selectedLandmark.visualResourceId }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sceneApi } from '../api/scene.js'

export default {
  name: 'ScenePage',
  data() {
    return {
      scenes: [],
      landmarks: [],
      selectedScene: null,
      selectedLandmark: null,
      showLandmarkModal: false,
      loading: true,
      error: null,
      defaultSceneImage: '/src/assets/default-scene.jpg',
      defaultLandmarkImage: '/src/assets/default-landmark.jpg'
    }
  },
  computed: {
    currentSceneIndex() {
      if (!this.selectedScene || !this.scenes.length) return 0
      return this.scenes.findIndex(scene => scene.sceneId === this.selectedScene.sceneId)
    }
  },
  async mounted() {
    await this.loadScenes()
  },
  methods: {
    async loadScenes() {
      try {
        this.loading = true
        this.error = null
        
        const response = await sceneApi.getAllScenes()
        if (response.code === 0) {
          // 处理场景背景图片URL，去除反引号和多余空格
          this.scenes = (response.data || []).map(scene => ({
            ...scene,
            sceneImageUrl: scene.sceneImageUrl ? scene.sceneImageUrl.replace(/`/g, '').trim() : ''
          }))
          // 默认选择第一个场景
          if (this.scenes.length > 0) {
            await this.selectScene(this.scenes[0])
          }
        } else {
          throw new Error(response.message || '获取场景列表失败')
        }
      } catch (error) {
        console.error('加载场景失败:', error)
        this.error = error.message || '加载场景失败，请稍后重试'
      } finally {
        this.loading = false
      }
    },

    async selectScene(scene) {
      try {
        this.selectedScene = scene
        this.landmarks = []
        
        // 加载该场景的地标
        const response = await sceneApi.getLandmarksBySceneId(scene.sceneId)
        if (response.code === 0) {
          // 处理图片URL，去除反引号和多余空格
          this.landmarks = (response.data || []).map(landmark => ({
            ...landmark,
            landmarkImageUrl: landmark.landmarkImageUrl ? landmark.landmarkImageUrl.replace(/`/g, '').trim() : ''
          }))
        }
      } catch (error) {
        console.error('加载地标失败:', error)
        // 不显示错误，只是地标为空
      }
    },

    showLandmarkDetail(landmark) {
      this.selectedLandmark = landmark
      this.showLandmarkModal = true
    },

    closeLandmarkModal() {
      this.showLandmarkModal = false
      this.selectedLandmark = null
    },

    exploreScene() {
      if (this.selectedScene) {
        // 这里可以添加场景探索的逻辑
        console.log('探索场景:', this.selectedScene.sceneName)
        alert(`开始探索 ${this.selectedScene.sceneName}！`)
      }
    }
  }
}
</script>

<style scoped>
.scene-page {
  min-height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  color: var(--text-primary);
  position: relative;
}

.page-header {
  text-align: center;
  padding: 2rem 0;
  background: rgba(0, 0, 0, 0.3);
}

.page-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  font-size: 1.1rem;
  color: var(--text-secondary);
  margin: 0;
}

.scene-container {
  display: flex;
  height: calc(100vh - 200px);
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 2rem;
}

/* 左侧场景列表 */
.scene-sidebar {
  width: 300px;
  background: transparent;
  /* backdrop-filter: blur(10px); */
  border: 1px solid var(--border-glass);
  border-radius: 12px;
  margin-right: 2rem;
  overflow: hidden;
}

.scene-nav {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.nav-title {
  padding: 1.5rem;
  font-size: 1.3rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border-glass);
  background: rgba(255, 255, 255, 0.02);
}

.scene-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0;
}

.scene-item {
  padding: 1rem 1.5rem;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.scene-item:hover {
  background: rgba(255, 255, 255, 0.05);
  border-left-color: var(--primary-color);
}

.scene-item.active {
  background: rgba(255, 107, 107, 0.1);
  border-left-color: var(--primary-color);
}

.scene-name {
  font-size: 1.1rem;
  font-weight: 500;
  margin-bottom: 0.3rem;
}

.scene-type {
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* 右侧场景详情 */
.scene-main-content {
  flex: 1;
  position: relative;
}

.scene-display {
  height: 100%;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
}

.scene-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

.scene-bg-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scene-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.7) 100%);
}

.scene-info {
  position: relative;
  z-index: 2;
  height: 100%;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.scene-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
}

.scene-description {
  font-size: 1.1rem;
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: 2rem;
  max-width: 600px;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
}

.landmarks-section {
  margin-bottom: 2rem;
}

.landmarks-title {
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 1rem;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
}

.landmarks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 1rem;
  max-width: 600px;
}

.landmark-item {
  background: var(--bg-glass);
  border: 1px solid var(--border-glass);
  border-radius: 8px;
  padding: 0.8rem;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
}

.landmark-item:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.landmark-image {
  width: 60px;
  height: 60px;
  margin: 0 auto 0.5rem;
  border-radius: 50%;
  overflow: hidden;
}

.landmark-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.landmark-name {
  font-size: 0.9rem;
  font-weight: 500;
}

.scene-actions {
  align-self: flex-start;
}

.explore-btn {
  background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
  color: white;
  padding: 1rem 2rem;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: 25px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

.explore-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}

.explore-icon {
  font-size: 1.2rem;
}

.scene-pagination {
  position: absolute;
  bottom: 2rem;
  right: 2rem;
  z-index: 3;
}

.page-indicator {
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.9rem;
  border: 1px solid var(--border-glass);
}

/* 占位符状态 */
.scene-placeholder {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-glass);
  border: 1px solid var(--border-glass);
  border-radius: 12px;
}

.placeholder-content {
  text-align: center;
}

.placeholder-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.placeholder-text {
  font-size: 1.2rem;
  color: var(--text-muted);
}

/* 加载和错误状态 */
.loading-container,
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 50vh;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.1);
  border-left: 4px solid var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.error-message {
  font-size: 1.1rem;
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

.retry-btn {
  background: var(--primary-color);
  color: white;
  padding: 0.8rem 1.5rem;
  border-radius: 8px;
  font-weight: 500;
}

.retry-btn:hover {
  background: #ff5252;
}

/* 地标详情弹窗 */
.landmark-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.landmark-modal-content {
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border-glass);
  border-radius: 12px;
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-glass);
}

.modal-header h3 {
  margin: 0;
  font-size: 1.5rem;
}

.close-btn {
  background: none;
  color: var(--text-primary);
  font-size: 1.5rem;
  padding: 0.5rem;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.modal-body {
  padding: 1.5rem;
}

.landmark-detail-image {
  width: 100%;
  height: 200px;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 1rem;
}

.landmark-detail-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.landmark-detail-info p {
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

.landmark-meta {
  border-top: 1px solid var(--border-glass);
  padding-top: 1rem;
}

.meta-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.meta-label {
  color: var(--text-muted);
}

.meta-value {
  color: var(--text-primary);
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .scene-container {
    flex-direction: column;
    height: auto;
  }
  
  .scene-sidebar {
    width: 100%;
    height: 200px;
    margin-right: 0;
    margin-bottom: 1rem;
  }
  
  .scene-list {
    display: flex;
    overflow-x: auto;
    padding: 1rem;
  }
  
  .scene-item {
    min-width: 200px;
    margin-right: 1rem;
  }
  
  .scene-main-content {
    height: 60vh;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .scene-container {
    padding: 0 1rem;
  }
  
  .scene-info {
    padding: 1.5rem;
  }
  
  .scene-title {
    font-size: 2rem;
  }
  
  .landmarks-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  }
}
</style>